package com.itheima.controller;

import com.itheima.entity.Result;
import com.itheima.entity.User;
import com.itheima.exception.CustomException;
import com.itheima.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/findById")
    public User findById(Integer id) {
        User user = userService.findById(id);
        return user;
    }

    @GetMapping("/{id}")
    public Result findById2(@PathVariable("id") Integer id) {
        //通过log变量,调用debug方法,在日志级别为debug级别下显示日志
        log.debug("findById {" + id + "}");
        User user = userService.findById(id);
        log.debug("user {" + user + "}");
        if (user == null) {
            throw new CustomException("该用户信息不存在");
        }
        return new Result(true,"查询用户数据成功",user);
    }
}
